$(function () {
    var
        _user,											//用户信息
        _query = {"id": getUrlParam("id")},			//请求条件·
        _html = [],										//html字符串
        _thisPage;										//当前页

    var orderDetailPage = function () {
        this.init();
    };
    orderDetailPage.prototype = {
        init: function () {
            this.loadData();
            this.bindEvent();
        },
        //加载数据
        loadData: function () {
            _user = getLocalStorage(STORAGE_MANAGER.user);
            if (!_user) {
                redirect("../account/login.html");
                return false;
            }
            else if (!_query.id) {
                $.toast("订单号为空");
                return false;
            }
            else {
                this.loadOrder();
            }
            ;
        },
        //请求订单数据
        loadOrder: function () {
            var requestUrl = CONFIG_MANAGER.RequstUrl + "v1/orders/" + _user.id;
            $.ajax({
                type: "GET",
                url: requestUrl,
                data: _query,
                async: true,
                headers: {"token": _user.token},
                beforeSend: function () {
                    $.showWaiting("等待中");
                }
            }).done(function (data) {
                setTimeout(function () {
                    $.closeWaiting();
                }, 500);
                _thisPage.loadOrderDetail(data);
            });
        },
        //加载订单详情
        loadOrderDetail: function (order) {
            _html = [];
            //商品信息
            _html.push('<div class="ddhbg">');
            _html.push('<p class="ddh"><span>订单号：' + order.id + '</span>已发货</p>');
            _html.push('</div>');

            _html.push('<div class="shdzbg">');
            //收货信息
            _html.push('<p class="shxxbt">收货信息</p>');
            _html.push('<div class="shdzbg">');
            _html.push('<div class="shdzk mbtopbottom">');
            _html.push('<p class="dwei"><img src="../../images/ditutb.png"/></p>');
            _html.push('<div class="shdzhi">');
            _html.push('<p class="sjrmc"><span>' + order.mobile + '</span>收件人：' + order.consignee + '</p>');
            _html.push('<p>' + order.address + '</p>');
            _html.push('<p><span>发票：' + order.invoice_title + '</span>备注：' + order.remark + '</p>');
            _html.push('</div>');
            _html.push('</div>');
            _html.push('</div>');
            //物流信息
            _html.push('<div class="wlxqbg">');
            _html.push('<p class="shxxbt">物流详情</p>');
            _html.push('<div class="wlxqnrbg mbtopbottom">');
            _html.push('<p class="wlxqnr mbbottom"><span>快递</span>物流方法</p>');
            _html.push('<p class="wlxqnr mbbottom"><span>圆通快递</span>物流公司</p>');
            _html.push('<p class="wlxqnr mbbottom"><span>219284755</span>运单号码</p>');
            _html.push('<p class="wlxqnr"><span>2015-8-20  8:30:20  到达杭州市西湖区<b class="icon-right">></b></span>物流信息</p>');
            _html.push('</div>');
            _html.push('</div>');
            //商品信息
            _html.push('<div class="wlxqbg">');
            _html.push('<div class="wlxqnrbg mbtopbottom">');
            $.each(order.items, function (i, goods) {
                _html.push('<p class="shxxbt1  mbbottom"><span>生活日用品商铺</span></p>');
                _html.push('<div class="mbbottom with100 overflow">');
                _html.push('<dl class="cpxqxztop cpxqxztop1 mtop0">');
                _html.push('<dt><img src="../../images/splbtu3.jpg" /></dt>');
                _html.push('<dd>');
                _html.push('<p class="floatr">');
                //星币
                _html.push('<span class="jinbi"><b><img src="../../images/jbtub.jpg"></b>70</span>+');
                _html.push('<span class="money">￥' + goods.act_price + '</span>');
                _html.push('</p>' + goods.sku_name + '');
                _html.push('</dd>');
                _html.push('<dd class="huisezi"><span class="x2">x2</span>' + goods.specs + '</dd>');
                _html.push('</dl>');
                _html.push('</div>');
            });

            _html.push('<p class="wlxqnr mbbottom"><span>￥' + order.goods_fee + '</span>商品金额</p>');
            _html.push('<p class="wlxqnr mbbottom"><span>￥' + order.delivery_fee + '</span>运费</p>');
            _html.push('<p class="wlxqnr mbbottom"><span class="jinbi huangzi"><b><img src="../../images/jbtub.jpg"></b>7</span>星币</p>');
            _html.push('<p class="wlxqnr mbbottom"><span>￥' + order.discount_fee + '</span>优惠</p>');
            _html.push('<p class="wlxqnr"><span class="hongzei">￥' + order.pay_fee + '</span>实付</p>');
            _html.push('</div>');
            _html.push('</div>');
            //下单时间
            _html.push('<p class="xdshijian"><span>下单时间:</span>' + toDateString(order.create_time, 'yyyy-MM-dd hh:mm') + '</p>');
            _html.push('<p class="xdshijian"><span>付款时间:</span>2015-8-20  17:29:01</p>');
            _html.push('<p class="xdshijian"><span>发货时间:</span>2015-8-20  17:29:01</p>');
            _html.push('</div>');
            $("#orderDetail").html(_html.join(''));
            //订单操作
            $("#orderDetail").after('<div class="footbg1 mbtopbottom"><p class="anniu"><a href="javascript:void(0);">确认收货</a></p></div>');
        },
        //页面事件
        bindEvent: function () {
            //滚动到底部显示向上图标
            $("#orderDetail").scroll(function () {
                var divHeight = $(this).height(), scrollTop = $(this)[0].scrollTop, scrollHeight = $(this)[0].scrollHeight;
                var paddingBottom = parseInt($(this).css('padding-bottom')), paddingTop = parseInt($(this).css('padding-top'));
                //console.log("divHeight:"+divHeight+"  scrollTop:"+scrollTop+" paddingBottom:"+paddingBottom+" paddingTop:"+paddingTop+" scrollHeight:"+scrollHeight);
                if ((divHeight + scrollTop + paddingBottom + paddingTop) >= scrollHeight) {
                    $("#gotop").show();
                } else {
                    $("#gotop").hide();
                }
                ;
            });

            //点击向上图标
            $("#gotop").on("tap", function () {
                $('#orderDetail').animate({scrollTop: '0px'}, 400);
            });
        },
    };
    _thisPage = new orderDetailPage();
});